package com.lixl.collection;

import java.math.BigDecimal;
import java.math.BigInteger;

public class Test01 {

	public static void main(String[] args) {
		BigDecimal m1 = new BigDecimal("7686369774870");
		System.out.println(m1.multiply(new BigDecimal("2465099745779"))
				.toString());

		BigDecimal m2 = new BigDecimal(
				"9167486769200391580986"
						+ "6092758538016248310668014430862240712651642793465704086709659327920576748"
						+ "080679002278301635492485238033574531693511190359657754734007568168830562"
						+ "0821016129132845564805780158806771");
		System.out.println(bigRoot(m2, 23, 5, BigDecimal.ROUND_HALF_UP));

	}

	public static BigDecimal bigRoot(BigDecimal number, int n, int scale,
			int roundingMode) {
		boolean negate = false;
		if (n < 0)
			throw new ArithmeticException();
		if (number.compareTo(BigDecimal.ZERO) < 0) {
			if (n % 2 == 0)
				throw new ArithmeticException();
			else {
				number = number.negate();
				negate = true;
			}
		}

		BigDecimal root;

		if (n == 0)
			root = BigDecimal.ONE;
		else if (n == 1)
			root = number;
		else {
			final BigInteger N = BigInteger.valueOf(n);
			final BigInteger N2 = BigInteger.TEN.pow(n);
			final BigInteger N3 = BigInteger.TEN.pow(n - 1);
			final BigInteger NINE = BigInteger.valueOf(9);

			BigInteger[] C = new BigInteger[n + 1];
			for (int i = 0; i <= n; i++) {
				C[i] = combination(n, i);
			}

			BigInteger integer = number.toBigInteger();
			String strInt = integer.toString();
			int lenInt = strInt.length();
			for (int i = lenInt % n; i < n && i > 0; i++)
				strInt = "0" + strInt;
			lenInt = (lenInt + n - 1) / n * n;
			BigDecimal fraction = number.subtract(number.setScale(0,
					BigDecimal.ROUND_DOWN));
			int lenFrac = (fraction.scale() + n - 1) / n * n;
			fraction = fraction.movePointRight(lenFrac);
			String strFrac = fraction.toPlainString();
			for (int i = strFrac.length(); i < lenFrac; i++)
				strFrac = "0" + strFrac;

			BigInteger res = BigInteger.ZERO;
			BigInteger rem = BigInteger.ZERO;
			for (int i = 0; i < lenInt / n; i++) {
				rem = rem.multiply(N2);

				BigInteger temp = new BigInteger(strInt.substring(i * n, i * n
						+ n));
				rem = rem.add(temp);

				BigInteger j;
				if (res.compareTo(BigInteger.ZERO) != 0)
					j = rem.divide(res.pow(n - 1).multiply(N).multiply(N3));
				else
					j = NINE;
				BigInteger test = BigInteger.ZERO;
				temp = res.multiply(BigInteger.TEN);
				while (j.compareTo(BigInteger.ZERO) >= 0) {
					// test = res.multiply(BigInteger.TEN);
					// test = ((test.add(j)).pow(n)).subtract(test.pow(n));
					test = BigInteger.ZERO;
					if (j.compareTo(BigInteger.ZERO) > 0)
						for (int k = 1; k <= n; k++)
							test = test.add(j.pow(k).multiply(C[k])
									.multiply(temp.pow(n - k)));
					if (test.compareTo(rem) <= 0)
						break;
					j = j.subtract(BigInteger.ONE);
				}

				rem = rem.subtract(test);
				res = res.multiply(BigInteger.TEN);
				res = res.add(j);
			}
			for (int i = 0; i <= scale; i++) {
				rem = rem.multiply(N2);

				if (i < lenFrac / n) {
					BigInteger temp = new BigInteger(strFrac.substring(i * n, i
							* n + n));
					rem = rem.add(temp);
				}

				BigInteger j;
				if (res.compareTo(BigInteger.ZERO) != 0) {
					j = rem.divide(res.pow(n - 1).multiply(N).multiply(N3));
				} else
					j = NINE;
				BigInteger test = BigInteger.ZERO;
				BigInteger temp = res.multiply(BigInteger.TEN);
				while (j.compareTo(BigInteger.ZERO) >= 0) {
					// test = res.multiply(BigInteger.TEN);
					// test = ((test.add(j)).pow(n)).subtract(test.pow(n));
					test = BigInteger.ZERO;
					if (j.compareTo(BigInteger.ZERO) > 0)
						for (int k = 1; k <= n; k++)
							test = test.add(j.pow(k).multiply(C[k])
									.multiply(temp.pow(n - k)));
					if (test.compareTo(rem) <= 0)
						break;
					j = j.subtract(BigInteger.ONE);
				}

				rem = rem.subtract(test);
				res = res.multiply(BigInteger.TEN);
				res = res.add(j);
			}
			root = new BigDecimal(res).movePointLeft(scale + 1);
			if (negate)
				root = root.negate();
		}
		return root.setScale(scale, roundingMode);
	}

	public static BigInteger combination(int n, int k) {
		if (k > n || n < 0 || k < 0)
			return BigInteger.ZERO;
		if (k > n / 2)
			return combination(n, n - k);
		BigInteger N1 = BigInteger.ONE;
		BigInteger N2 = BigInteger.ONE;
		BigInteger N = BigInteger.valueOf(n);
		BigInteger K = BigInteger.valueOf(k);
		for (int i = 0; i < k; i++) {
			N1 = N1.multiply(N);
			N2 = N2.multiply(K);
			N = N.subtract(BigInteger.ONE);
			K = K.subtract(BigInteger.ONE);
		}
		return N1.divide(N2);
	}

}
